#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1e5 + 10;
typedef long long LL;

LL n;
LL a[N];

int main()
{
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> a[i];

	sort(a + 1, a + 1 + n);

	LL ret = 0;
	for(int i = 1; i <= n; i++)
	{
		//中间位置的下标是 (1 + n) / 2
		ret += abs(a[i] - a[(n + 1) / 2]);
	}
	cout << ret << endl;
	

	return 0;
}